package jonaArgumentationSchemes;

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

import java.util.*;


public class KnowledgeEditor extends JFrame implements ActionListener, MouseListener {

	JScrollPane jspKnowledgeName,jspKnowledgeText;
	JTextArea knowledgeText;
	
	JToolBar jtb;
	JButton saveBtn, addBtn, removeBtn;
	
	DefaultListModel listModel = new DefaultListModel();
	JList knowledgeList;
	
	public KnowledgeEditor()
	{
		// Buttons
		saveBtn = new JButton("Save");
		addBtn = new JButton("Add");
		removeBtn = new JButton("Remove");
		saveBtn.addActionListener(this);
		addBtn.addActionListener(this);
		removeBtn.addActionListener(this);
		//buttonPane.setSize(600,130);
		jtb = new JToolBar();
		jtb.add(saveBtn);
		jtb.add(addBtn);
		jtb.add(removeBtn);
		
		// Textfield and list
		knowledgeText = new JTextArea();
		knowledgeList = new JList(listModel);
		jspKnowledgeName = new JScrollPane(knowledgeList);
		jspKnowledgeText = new JScrollPane(knowledgeText);
		knowledgeList.addMouseListener(this);
		
		// Create the GUI.
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(jspKnowledgeName,BorderLayout.WEST);
		getContentPane().add(jspKnowledgeText,BorderLayout.CENTER);
		getContentPane().add(jtb,BorderLayout.SOUTH);
		this.setTitle("Knowledge Editor");
	   	int inset = 200;
        setBounds(inset, inset, 600, 420);
        
        updateList();
	}
	
	public void updateList()
	{
		listModel.clear();
		for (Enumeration e = Knowledge.getKnowledge() ; e.hasMoreElements() ;)
		{
			Object name = e.nextElement();
			listModel.addElement((String) name);
		}
		
		// update the list throughtout the whole program.
		Knowledge.updateKnowledgeList();
	}
	

	public static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(false);
   		
        //Create and set up the window.
		KnowledgeEditor frame = new KnowledgeEditor();
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Display the window.
        frame.setVisible(true);
    }
	
	
	// Actionlistener
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == saveBtn)
		{   
			// Confirmation dialog
			JDialog dialog = new JDialog();
			int choice = JOptionPane.showConfirmDialog (dialog,
					"Do you really want to Save '"+(String) knowledgeList.getSelectedValue()+"'?\n" +
							"This will destroy the old values." ,
					"Are you sure?" , JOptionPane.YES_NO_OPTION);
			if(choice!=JOptionPane.YES_OPTION)
						return;
			
			// Save the new Knowledge
			Knowledge.addKnowledge((String) knowledgeList.getSelectedValue(),knowledgeText.getText());
			Knowledge.save();
		}
		else if (e.getSource() == addBtn)
		{   
			// Input Dialog
			JDialog dialog = new JDialog();
			String newEntry = JOptionPane.showInputDialog(dialog,"Please enter the name of the new Knowledge Entry"
								,"Add new Knowledge",JOptionPane.OK_CANCEL_OPTION);
			System.out.println(newEntry);
			if (newEntry==null)
				return;
			
			// Add the new entry
			Knowledge.addKnowledge(newEntry,"");
			updateList();
			
			// Put cursor on new entry.
			knowledgeList.setSelectedValue(newEntry,true);
		}
		else if (e.getSource() == removeBtn)
		{   
			// Confirmation dialog
			JDialog dialog = new JDialog();
			int choice = JOptionPane.showConfirmDialog (dialog,
					"Do you really want to Remove '"+(String) knowledgeList.getSelectedValue()+"'?" ,
					"Are you sure?" , JOptionPane.YES_NO_OPTION);
			if(choice!=JOptionPane.YES_OPTION)
						return;
			
			// Remove the selected Knowledge entry
			Knowledge.removeKnowledge((String) knowledgeList.getSelectedValue());
			Knowledge.save();
			knowledgeText.setText("");
			updateList();
		}
	}
	
	
	//	The mouselistener.
	public void mouseClicked(MouseEvent e) {
		  if (e.getClickCount() == 1) {
			  knowledgeText.setText(Knowledge.getPlainKnowledge((String) knowledgeList.getSelectedValue()));
		  }     
	 }
	
	public void mousePressed(MouseEvent e) {};
	
	public void mouseReleased(MouseEvent e) {};
	
	public void mouseEntered(MouseEvent e) {};
	
	public void mouseExited(MouseEvent e) {};

}
